<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
<script>
    function Node(data,left,right){
        this.data=data;
        this.left=left;
        this.right=right;
    }


    function BST(){
        this.root=null;
    }
    BST.prototype={
        insert: function (data) {
            var n=new Node(data,null,null),curr,parent;
            if(null==this.root){
                this.root=n;
            }else{
                curr=this.root;
                while(true){
                    parent=curr;
                    if(data<curr.data){
                        curr=parent.left;
                        if(null==curr){
                            parent.left=n;
                            break;
                        }
                    }else{
                        curr=curr.right;
                        if(null==curr){
                            parent.right=n;
                            break;
                        }
                    }
                }
            }
        },
        show:function(){
            console.info('show:');
            console.log(JSON.stringify(this));
            console.dir(this);
        },
        inOrder: function (node) {
            console.group('inOrder');
            if(null!==node){
                this.inOrder(node.left);
                console.log(node.show());
                this.inOrder(node.right);
            }
            console.groupEnd('inOrder');
        },
        preOrder: function (node) {
            console.group('preOrder');
            if(null!==node){
                this.preOrder(node.left);
                console.log(node.show());
                this.preOrder(node.right);
            }
            console.groupEnd('preOrder');
        },
        postOrder: function (node) {
            console.group('postOrder');
            if(null!==node){
                this.postOrder(node.left);
                console.log(node.show());
                this.postOrder(node.right);
            }
            console.groupEnd('postOrder');
        },

        getMin: function () {
            var curr=this.root;
            while(null!==curr.left){
                curr=curr.left;
            }
            return curr.data;
        },

        getMax:function(){
            var curr=this.root;
            while(null!==curr.right){
                curr=curr.right;
            }
            return curr.data;
        },
        find:function(data){
            var curr=this.root;
            while(null!==curr){
                if(curr.data==data)return curr;
                if(data<curr.data){
                    curr=curr.left;
                }else{
                    curr=curr.right;
                }
            }
            return null;
        }
    }


    var nums=new BST();
    nums.insert(5);
    nums.insert(5);
    nums.insert(3);
    nums.insert(6);
    nums.insert(1);
    nums.insert(8);
    nums.insert(0);
    nums.insert(7);
    nums.insert(-1);
    nums.insert(-1);
    nums.insert(10);
    nums.insert(2);
    nums.show();

    //nums.inOrder(nums.root);
    //nums.preOrder(nums.root);
    //nums.postOrder(nums.root);

    console.info('min:',nums.getMin());
    console.info('min:',nums.getMax());

    console.info('find:',nums.find(1));

</script>
</body>
</html>